define(['jquery', 'bootstrap', 'form', 'table', 'template'], function ($, undefined, Form, Table, template) {
    var Controller = {
        index: function () {
            var table = $("#table");
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: '/content/article/index' + location.search,
                    add_url: '/content/article/add',
                    edit_url: '/content/article/edit',
                    del_url: '/content/article/del',
                    table: 'article',
                }
            });

            $(table).bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                columns: [
                    {checkbox: true},
                    {field: 'id', title: 'ID',},
                    {field: 'title', title: '标题'},
                    {field: 'cover', title: '封面图', formatter: Table.api.formatter.images},
                    {
                        field: 'operate',
                        title: '操作',
                        table: table,
                        events: Table.api.events.operate,
                        formatter: Table.api.formatter.operate
                    }],
            })
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        recyclebin: function () {
            var table = $("#table");
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    recyclebin_url: '/content/article/recycleBin',
                }
            });
            $(table).bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.recyclebin_url,
                columns: [
                    {checkbox: true,},
                    {field: 'id', title: 'ID',},
                    {field: 'title', title: '分类名',},
                    {field: 'delete_time', title: '删除时间'},
                    {
                        field: 'operate',
                        title: '操作',
                        table: table,
                        events: Table.api.events.operate,
                        formatter: Table.api.formatter.operate,
                        buttons: [
                            {
                                name: 'Restore',
                                text: '还原',
                                classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                icon: 'fa fa-rotate-left',
                                url: '/content/article/restore',
                                confirm: '是否确定还原该项',
                                refresh: true
                            },
                            {
                                name: 'Destroy',
                                text: '彻底删除',
                                classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                icon: 'fa fa-times',
                                url: '/content/article/destroy',
                                confirm: '是否确定彻底删除该项',
                                refresh: true
                            }
                        ],
                    }],
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        events: {
            get_img: function () {
                $(document).on('click', '.btn-getimg', function () {
                    //获取文章内容
                    KindEditor.sync('#content');
                    var content = $("#content").val();
                    var regexp = /<img\b.*?(?:\>|\/>)/g;
                    var result = content.match(regexp);
                    console.log(result);
                    var img_str_list = [];
                    $.each(result, function (k, v) {
                        let src = $(v).attr('src');
                        img_str_list.push(src);
                    })
                    var html = template('img-list', {img_str_list});

                    layer.open({
                        type: 1,
                        title: '选择图片',
                        content: html,
                        area: ['550px', '450px'],
                        resize: false,
                        btn: ['确认', '取消'],
                        yes: function (index, layero) {
                            let img = $(".img-box li.file-item-select").find('img');
                            let src = img.attr('src');
                            let current = $("#c-cover").val();
                            let img_list = current.split(',');
                            if (img_list.length >3) {
                                Toastr.error('最多只允许选择3张封面图');
                                return false;
                            }
                            img_list.push(src);
                            $("#c-cover").val(img_list).trigger('change');
                            layer.close(index);

                        }, btn2: function (index, layero) {
                            layer.close(index);
                        }
                    })
                    ;

                    $(document).on('click', ".img-box li", function () {
                        let img = $(this).find('img');
                        $(this).addClass('file-item-select').siblings().removeClass('file-item-select');
                    })


                });
            }
        },
        api: {
            bindevent: function () {
                var form = $('form[role="form"]');
                Form.api.bindevent(form);
                $(document).on('click', '.btn-tag', function () {
                    KindEditor.sync('#content');
                    var title = $("#title").val();
                    var content = $("#content").val();
                    if (title == '' || content == '') {
                        layer.msg('标题和内容不能为空！');
                        return false;
                    }
                    Aojie.api.axios({
                        url: '/content/article/tags',
                        method: 'POST',
                        data: {title: title, content: content},
                    }, function (ret) {
                        var value = $("#c-tag").val();
                        var tmp = [];
                        if (value) {
                            tmp = value.split(',')
                        }
                        tmp = tmp.concat(ret.tags)
                        $("#c-tag").val(tmp);
                    })

                });

                Controller.events.get_img();
            }

        }


    };
    return Controller;
});
